package hu.diploma.android.serviceimpl;

import hu.diploma.android.util.TypeEnumMapper;
import hu.diploma.common.service.AddressService;
import hu.diploma.common.vos.AddressVo;
import hu.diploma.common.vos.PersonVo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.util.Log;

public class AddressServiceImpl implements AddressService {
	ContentResolver contentResolver;

	public AddressServiceImpl(ContentResolver contentResolver) {
		super();
		this.contentResolver = contentResolver;
	}

	@Override
	public AddressVo save(AddressVo addressVo) {
		ContentValues values = new ContentValues();
		values.put(Data.RAW_CONTACT_ID, addressVo.getPerson().getPhoneId());
		values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
		values.put(StructuredPostal.POBOX, addressVo.getPobox());
		values.put(StructuredPostal.STREET, addressVo.getStreet());
		values.put(StructuredPostal.CITY, addressVo.getCity());
		values.put(StructuredPostal.REGION, addressVo.getState());
		values.put(StructuredPostal.POSTCODE, addressVo.getPostalCode());
		values.put(StructuredPostal.COUNTRY, addressVo.getCountry());
		values.put(StructuredPostal.TYPE, TypeEnumMapper.getAddressEnumMap().get(addressVo.getType()));

		Uri uri = this.contentResolver.insert(Data.CONTENT_URI, values);
		long id = ContentUris.parseId(uri);
		addressVo.setPhoneId(id);

		return addressVo;
	}

	@Override
	public AddressVo merge(AddressVo addressVo) {
		ContentValues values = new ContentValues();
		values.put(Data.RAW_CONTACT_ID, addressVo.getPerson().getPhoneId());
		values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
		values.put(StructuredPostal.POBOX, addressVo.getPobox());
		values.put(StructuredPostal.STREET, addressVo.getStreet());
		values.put(StructuredPostal.CITY, addressVo.getCity());
		values.put(StructuredPostal.REGION, addressVo.getState());
		values.put(StructuredPostal.POSTCODE, addressVo.getPostalCode());
		values.put(StructuredPostal.COUNTRY, addressVo.getCountry());
		values.put(StructuredPostal.TYPE, TypeEnumMapper.getAddressEnumMap().get(addressVo.getType()));

		if (addressVo.getPhoneId() != null) {
			this.contentResolver.update(Data.CONTENT_URI, values, BaseColumns._ID + "=?", new String[] { addressVo.getPhoneId().toString() });

		} else {

			Uri uri = this.contentResolver.insert(Data.CONTENT_URI, values);
			long id = ContentUris.parseId(uri);
			addressVo.setPhoneId(id);
		}
		return addressVo;
	}

	@Override
	public AddressVo findById(Serializable searchId) {
		Log.d(this.getClass().getCanonicalName(), "Start: loadAddress ");
		AddressVo addressVo = new AddressVo();
		String where = BaseColumns._ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
		String[] whereParameters = new String[] { searchId.toString(), ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
		Cursor addrCur = this.contentResolver.query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null);

		try {

			if (addrCur.moveToFirst()) {
				String poBox = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
				String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
				String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
				String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
				String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
				String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
				String type = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

				addressVo.setPobox(poBox);
				addressVo.setStreet(street);
				addressVo.setCity(city);
				addressVo.setState(state);
				addressVo.setPostalCode(postalCode);
				addressVo.setCountry(country);
				addressVo.setType(TypeEnumMapper.getAddressTypeMap().get(type));

			}
		} finally {
			addrCur.close();
		}
		Log.d(this.getClass().getCanonicalName(), "End: loadAddress ");
		return addressVo;
	}

	@Override
	public List<AddressVo> loadAll() {
		Log.d(this.getClass().getCanonicalName(), "Start: loadAddress ");
		ArrayList<AddressVo> addressVos = new ArrayList<AddressVo>();
		String where = ContactsContract.Data.MIMETYPE + " = ?";
		String[] whereParameters = new String[] { ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
		Cursor addrCur = this.contentResolver.query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null);

		try {

			while (addrCur.moveToNext()) {
				String poBox = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
				String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
				String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
				String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
				String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
				String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
				String type = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

				AddressVo addressVo = new AddressVo();
				addressVo.setPobox(poBox);
				addressVo.setStreet(street);
				addressVo.setCity(city);
				addressVo.setState(state);
				addressVo.setPostalCode(postalCode);
				addressVo.setCountry(country);
				addressVo.setType(TypeEnumMapper.getAddressTypeMap().get(type));
				addressVos.add(addressVo);
			}
		} finally {
			addrCur.close();
		}
		Log.d(this.getClass().getCanonicalName(), "End: loadAddress ");
		return addressVos;
	}

	@Override
	public void delete(AddressVo addressVo) {
		String where = BaseColumns._ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
		String[] whereParameters = new String[] { addressVo.getPhoneId().toString(), ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
		this.contentResolver.delete(ContactsContract.Data.CONTENT_URI, where, whereParameters);

	}

	@Override
	public List<AddressVo> findByPeople(PersonVo peopleVo) {
		Log.d(this.getClass().getCanonicalName(), "Start: loadAddress " + peopleVo.getPhoneId() + this.contentResolver);

		String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
		String[] whereParameters = new String[] { peopleVo.getPhoneId().toString(), ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
		Cursor addrCur = this.contentResolver.query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null);

		List<AddressVo> addressVos = new ArrayList<AddressVo>();
		try {

			while (addrCur.moveToNext()) {
				String poBox = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
				String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
				String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
				String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
				String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
				String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
				String type = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

				AddressVo addressVo = new AddressVo();
				addressVo.setPobox(poBox);
				addressVo.setStreet(street);
				addressVo.setCity(city);
				addressVo.setState(state);
				addressVo.setPostalCode(postalCode);
				addressVo.setCountry(country);
				addressVo.setType(TypeEnumMapper.getAddressTypeMap().get(type));
				addressVos.add(addressVo);
			}
		} finally {
			addrCur.close();
		}
		Log.d(this.getClass().getCanonicalName(), "End: loadAddress ");
		return addressVos;
	}

	@Override
	public AddressVo findByPhoneId(Serializable searchId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
